#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// global header structure for a .pcap file 
// (adapted from http://wiki.wireshark.org/Development/LibpcapFileFormat; types changed to reflect local env)
typedef struct pcap_hdr_s {
        unsigned int magic_number;   /* magic number */
        unsigned short version_major;  /* major version number */
        unsigned short version_minor;  /* minor version number */
        int  thiszone;       /* GMT to local correction */
        unsigned int sigfigs;        /* accuracy of timestamps */
        unsigned int snaplen;        /* max length of captured packets, in octets */
        unsigned int network;        /* data link type */
} pcap_hdr_t;

// packet header -- as captured by wireshark
// (adapted from http://wiki.wireshark.org/Development/LibpcapFileFormat; types changed to reflect local env)
typedef struct pcaprec_hdr_s {
        unsigned int ts_sec;         /* timestamp seconds */
        unsigned int ts_usec;        /* timestamp microseconds */
        unsigned int incl_len;       /* number of octets of packet saved in file */
        unsigned int orig_len;       /* actual length of packet */
} pcaprec_hdr_t;

//Struct for the ethernet frame
typedef struct ethernet_frame {
		unsigned char mac_dst[6];
        unsigned char mac_src[6]; 
        unsigned char fluff[14];
		unsigned char ip_src[4];
		unsigned char ip_dst[4];
		unsigned char tcp_src[2];
		unsigned char tcp_dst[2];
};

/*
 * main() is the starting point for all C and C++ programs.
 */
int main()
{
	// input file pointer
	FILE* input;

	// put your program's variable declarations here

	struct pcap_hdr_s globalHeader;
	struct pcaprec_hdr_s packetHeader;
	struct ethernet_frame ethernetFrame;

	int i;
	unsigned short tcp_src_final;
	unsigned short tcp_dst_final;
	
	// open the input file
	input = fopen("packets.pcap", "rb");  // the "rb" stands for "read binary"  
	//input = fopen("testpacket.pcap", "rb");

	/* an example of how to read from a binary file:
	
		fread(read_buffer, 10, 1, input);

		The above line reads from the file pointer named 'input' and places the results in memory starting at the
		location given by 'read_buffer' (usually a pointer variable).  It reads 1 chunk of memory that is 10 bytes
		long -- so this means that you must have allocated 10 bytes of memory starting at address 'read_buffer'!
	*/


	// start your program code here

	// read the global header
	fread(&globalHeader, sizeof(globalHeader), 1, input);
	
	// read the packet header
	fread(&packetHeader, sizeof(packetHeader), 1, input);
	
	// read the packet data (which should be our ETH packet); Don't forget to allocate space for it!
	fread(&ethernetFrame, sizeof(ethernetFrame), 1, input);

	//Prints out the source MAC address
	for(i = 0; i < 5; i++)
	{
		if(ethernetFrame.mac_src[i] < 0x10)
		{
			printf("0%x-",ethernetFrame.mac_src[i]);
		}
		else{
			printf("%x-",ethernetFrame.mac_src[i]);
		}
	}

	if(ethernetFrame.mac_src[5] < 0x10)
		{
			printf("0%x:",ethernetFrame.mac_src[i]);
		}
	else{
			printf("%x:",ethernetFrame.mac_src[i]);
		}
	
	//Prints out the source ip address
	for(i = 0; i < 3; i++)
	{
		printf("%d.",ethernetFrame.ip_src[i]);
	}
	printf("%d:",ethernetFrame.ip_src[3]);

	tcp_src_final = ethernetFrame.tcp_src[0] * 16 * 16 + ethernetFrame.tcp_src[1];

	//Prints out the source port
	printf("%d",tcp_src_final);

	//New line
	printf("\n");

	//Prints out the destination MAC address
	for(i = 0; i < 5; i++)
	{
		if(ethernetFrame.mac_dst[i] < 0x10)
		{
			printf("0%x-",ethernetFrame.mac_dst[i]);
		}
		else{
			printf("%x-",ethernetFrame.mac_dst[i]);
		}
	}

	//Prints out the destination ip address
	if(ethernetFrame.mac_dst[5] < 0x10)
		{
			printf("0%x:",ethernetFrame.mac_dst[i]);
		}
	else{
			printf("%x:",ethernetFrame.mac_dst[i]);
		}

	for(i = 0; i < 3; i++)
	{
		printf("%d.",ethernetFrame.ip_dst[i]);
	}
	printf("%d:",ethernetFrame.ip_dst[3]);

	tcp_dst_final = ethernetFrame.tcp_dst[0] * 16 * 16 + ethernetFrame.tcp_dst[1];

	//Prints out the destination port
	printf("%d\n",tcp_dst_final);

	fclose(input);

	// main() must return a value; zero means the program ended without error.
	return 0;
}